﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using System.Linq;
using WarehouseManagement.BusinessLogic.DTO;

namespace WarehouseManagement.Views.Merchandise
{
    public partial class MerchandiseCategoryList : DevExpress.XtraEditors.XtraForm
    {
        private MerchandiseBC _merchandiseBC;

        public MerchandiseCategoryList()
        {
            InitializeComponent();
            _merchandiseBC = new MerchandiseBC();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

        }

        private void MerchandiseCategoryList_Load(object sender, EventArgs e)
        {

            BindCategoryList();
            grvCategoryList_FocusedRowChanged(null, null);
        }

        private void BindCategoryList()
        {
            var list = _merchandiseBC.GetCategoryList().ToList();
            grdCategoryList.DataSource = list;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(false);
        }
        private void ShowAddEditForm(bool editMode)
        {
            NewEditMerchandiseCategory frm = new NewEditMerchandiseCategory(editMode);
            if (editMode)
            {
                var obj = GetFocusObject();
                frm.Id = obj.Id;
                frm.CategoryName = obj.MerchandiseCategoryName;
                frm.Notes = obj.Notes;
            }
            frm.OnOperationCompleted = new NewEditMerchandiseCategory.OperationCompleted(onOperationCompleted);
            frm.ShowDialog();
        }
        private void onOperationCompleted(bool success, bool isEditMode)
        {
            if (success)
            {
                BindCategoryList();
            }
            else
            {
                XtraMessageBox.Show("Đã xảy ra lỗi khi thực hiện thao tác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ShowAddEditForm(true);
        }

        private void grvCategoryList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var obj = GetFocusObject();
            btnDelete.Enabled = btnUpdate.Enabled = obj != null;
        }

        private MerchandiseCategoryDTO GetFocusObject()
        {
            return grvCategoryList.GetFocusedRow() as MerchandiseCategoryDTO;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có muốn xóa nhóm hàng này?", "Xóa nhóm hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                var obj = GetFocusObject();
                _merchandiseBC.DeleteCategory(obj.Id);
                BindCategoryList();
            }
            catch
            {
                XtraMessageBox.Show("Không thể xóa nhóm hàng lúc này", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                _merchandiseBC.RecycleContext();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}